package edu.bw.controller.param;

import edu.bw.vo.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;
import java.util.stream.Stream;

@Controller
@RequestMapping("/user")
public class UserController {

    /**
     * 默认类型的参数
     * @param request
     * @param response
     * @param Session
     * @param model
     * @return
     */
    @RequestMapping("/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response,
                       HttpSession Session, Model model){
        System.out.println(request);
        System.out.println(response);
        System.out.println(Session);
        System.out.println(model);
        return "userList";
    }

    /**
     * 基本(简单)类型参数
     * 如果直接提交username和password会导致参数绑定异常
     * 解决方案:
     * 1.利用@RequestParam注解执行提交的参数绑定到具体哪个形参上
     * 2.在maven工程中配置,让其在编译时保留参数名
     * @return
     */
    @PostMapping("/submitBaseType.do")
    public String submitBaseType(@RequestParam("name") String username,@RequestParam("password") String password){
        System.out.println(username+" "+password);
        return "success";
    }

    @PostMapping("/submitBaseType02.do")
    public String submitBaseType02(@RequestParam("name") String username,String password){
        System.out.println(username+" "+password);
        return "success";
    }


    /**
     * VO类型的参数绑定:前端提交的参数需要类中的属性一一对应才能完成VO绑定
     * @param user
     * @return
     */
    @PostMapping("submitVO.do")
    public String submitVo(User user){
        System.out.println(user);
        return "success";
    }


    /**
     * VO类型包装类的参数绑定
     * @param user
     * @return
     */
    @PostMapping("submitVO02.do")
    public String submitVO02(User user){
        System.out.println(user);
        return "success";
    }


    /**
     * 数组类型的参数绑定
     * @param hobby
     * @return
     */
    @PostMapping("submitArray.do")
    public String submitArray(String[] hobby){
//        System.out.println(Arrays.toString(hobby));
        Stream.of(hobby).forEach(System.out::println);

        return "success";
    }


    /**
     * 列表参数类型绑定  需要使用@RequestParam注解进行参数绑定
     * @param hobby
     * @return
     */
    @PostMapping("/submitList.do")
    public String submitList(@RequestParam List<String> hobby){
        hobby.forEach(System.out::println);
        return "success";
    }

    /**
     * map集合参数类型绑定:需要使用@RequestParam注解进行参数绑定
     * @param map
     * @return
     */
    @PostMapping("/submitMap.do")
    public String submitMap(@RequestParam Map<String,String> map){
//        System.out.println(map);
        map.forEach((key,value)-> System.out.println(key+"="+value));
        return "success";
    }



}
